//
//  PZLYiweiViewController.m
//  Various
//
//  Created by dllo on 16/3/29.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "PZLYiweiViewController.h"
#import "HLNetWorkTool.h"
#import "PZLCateCameraModel.h"
#import "PZLCateCameraTableViewCell.h"
#import "PZLSecondWebViewController.h"
@interface PZLYiweiViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *modelArr;
@property (nonatomic, strong)NSDictionary *dicInfo;


@end

@implementation PZLYiweiViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatSubview];
    [self creatMjRefresh];
}




- (void)creatMjRefresh{
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(creatNet)];
    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(creatFoot)];
    [self.tableView reloadData];
    
    
    
}



- (void)creatNet{
//    NSLog(@"%@", self.url);
    [HLNetWorkTool getWithURL:self.url Parameter:nil HttpHeader:nil ResponseType:ResponseTypeJSON Progress:^(NSProgress *progress) {
        
        
    } Success:^(id result) {
        self.modelArr = [NSMutableArray array];
        NSDictionary *dicData = [result objectForKey:@"data"];
        NSArray *array = [dicData objectForKey:@"articles"];
        self.dicInfo = [dicData objectForKey:@"info"];
        for (NSDictionary *dict in array) {
            PZLCateCameraModel *model = [PZLCateCameraModel baseModelWithDict:dict];
            if (model.media.count != 0) {
                [self.modelArr addObject:model];
            }
            
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } Failure:^(NSError *error) {
        
        
    }];
}




- (void)creatFoot{
    
    [HLNetWorkTool getWithURL:[self.dicInfo objectForKey:@"next_url"]  Parameter:nil HttpHeader:nil ResponseType:ResponseTypeJSON Progress:^(NSProgress *progress) {
        
        
    } Success:^(id result) {
        NSDictionary *dicData = [result objectForKey:@"data"];
        NSArray *array = [dicData objectForKey:@"articles"];
        self.dicInfo = [dicData objectForKey:@"info"];
        for (NSDictionary *dict in array) {
            PZLCateCameraModel *model = [PZLCateCameraModel baseModelWithDict:dict];
            if (model.media.count != 0) {
                [self.modelArr addObject:model];
            }
            
        }
        
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        
    } Failure:^(NSError *error) {
        
        
    }];
    
}






- (void)creatSubview{
    
    UIImage *image = [UIImage imageNamed:@"tqt_l_button"];
    // 取消渲染效果
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem = leftBarButton;

    
    
    
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, sWEIGHT*414, sHEIGHT*736) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 280;
    
    [self.view addSubview:self.tableView];
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"text";
    PZLCateCameraTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[PZLCateCameraTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    PZLCateCameraModel *model = self.modelArr[indexPath.row];
    
    cell.model = model;
    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PZLCateCameraModel *model = self.modelArr[indexPath.row];
    PZLSecondWebViewController *secondWebVC = [PZLSecondWebViewController new];
    secondWebVC.temp = model.weburl;
    [self.navigationController pushViewController:secondWebVC animated:YES];
}


- (void)goBack{
    [self.navigationController popToRootViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
